/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.sau.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.entity.AssetSau;
import com.fuyao.cloud.asset.vo.SauImportVO;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.sau.service.AssetSauService;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 标准资产型号
 *
 * @author liyue
 * @date 2021-11-17 17:29:51
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyassetsau" )
@Api(value = "fyassetsau", tags = "标准资产型号管理")
public class AssetSauController {

    private final AssetSauService fyAssetSauService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fyAssetSau 标准资产型号
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_view')" )
    public R getFyAssetSauPage(Page page, AssetSau fyAssetSau) {
        return R.ok(fyAssetSauService.page(page, Wrappers.query(fyAssetSau)));
    }

    @ApiOperation(value = "标准型号关联资产分类分页查询", notes = "标准型号关联资产分类分页查询")
    @GetMapping("/sauPage" )
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_view')" )
    public R sauPage(Page page, AssetSau fyAssetSau) {
        return R.ok(fyAssetSauService.sauPage(page, Wrappers.query(fyAssetSau),fyAssetSau));
    }

	/**
	 * 通过id查询标准资产型号
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/sau/{id}" )
	@PreAuthorize("@pms.hasPermission('sau_fyassetsau_view')" )
	public R getSauById(@PathVariable("id" ) Long id) {
		return R.ok(fyAssetSauService.getSauById(id));
	}


    /**
     * 通过id查询标准资产型号
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fyAssetSauService.getById(id));
    }

    /**
     * 新增标准资产型号
     * @param fyAssetSau 标准资产型号
     * @return R
     */
    @ApiOperation(value = "新增标准资产型号", notes = "新增标准资产型号")
    @SysLog("新增标准资产型号" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_add')" )
    public R save(@RequestBody AssetSau fyAssetSau) {
        fyAssetSauService.save(fyAssetSau);
        return R.ok(fyAssetSau);
    }

    /**
     * 修改标准资产型号
     * @param fyAssetSau 标准资产型号
     * @return R
     */
    @ApiOperation(value = "修改标准资产型号", notes = "修改标准资产型号")
    @SysLog("修改标准资产型号" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_edit')" )
    public R updateById(@RequestBody AssetSau fyAssetSau) {
        return R.ok(fyAssetSauService.updateById(fyAssetSau));
    }

    /**
     * 通过id删除标准资产型号
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除标准资产型号", notes = "通过id删除标准资产型号")
    @SysLog("通过id删除标准资产型号" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fyAssetSauService.removeById(id));
    }

    @ApiOperation(value = "批量删除标准资产型号管理", notes = "批量删除标准资产型号管理")
    @SysLog("批量删除标准资产型号管理" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return R.ok(fyAssetSauService.removeByIds(ids),String.format("成功删除%s条数据",ids.size()));
    }


    /**
     * 导出excel 表格
     * @param fyAssetSau 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('sau_fyassetsau_export')" )
    public List export(AssetSau fyAssetSau) {
        return fyAssetSauService.listSau(Wrappers.query(fyAssetSau));
    }

	/**
	 * 下载本地导入模板
	 * 获取本地（resources）文件
	 * @param fileName 文件名称
	 * @param response 本地文件
	 */
	@SneakyThrows
	@GetMapping("/local/{fileName}")
	@PreAuthorize("@pms.hasPermission('sau_fyassetsau_export')")
	public void localFile(@PathVariable String fileName, HttpServletResponse response) {
		ClassPathResource resource = new ClassPathResource("file/" + fileName);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IoUtil.copy(resource.getInputStream(), response.getOutputStream());
	}

	/**
	 * 导入资产分类信息信信息
	 * @param excelVOList 区域列表
	 * @param bindingResult 错误信息列表
	 * @return R
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('sau_fyassetsau_export')")
	public R importCti(@RequestExcel List<SauImportVO> excelVOList, BindingResult bindingResult) {
		return this.fyAssetSauService.importSau(excelVOList, bindingResult);
	}

	@GetMapping(value = "/tree")
	public R getTree(AssetSau fyAssetSau) {
		return R.ok(fyAssetSauService.selectTree(Wrappers.query(fyAssetSau)));
	}

    @ApiOperation(value = "创建方法标准资产型号管理", notes = "创建方法标准资产型号管理")
    @PostMapping(value = "/create")
	public R create() {
        AssetSau assetSau = new AssetSau();
        assetSau.setUnid(UUID.randomUUID().toString());
		return R.ok(assetSau);
	}

}
